﻿// -----------------------------------------------------------------------
// <copyright file="SystemConfig.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Xml.Linq;

namespace BasicFrameworkDemo.Utility
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// system config class
    /// </summary>
    public class SystemConfig
    {
        /// <summary>
        /// Gets the errors config file path.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/14/2012 5:21 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string ErrorsConfigFilePath
        {
            get
            {
                return GetAppSetting("ErrorsConfigPath");
            }
        }

        /// <summary>
        /// Gets the name of the errors config file root node.
        /// </summary>
        /// <value>
        /// The name of the errors config file root node.
        /// </value>
        /// Creator:LiLei
        /// CreationDate:7/14/2012 5:21 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string ErrorsConfigFileRootNodeName
        {
            get { return GetAppSetting("ErrorsConfigRootNodeName"); }
        }

        /// <summary>
        /// Gets the name of the errors config file child node.
        /// </summary>
        /// <value>
        /// The name of the errors config file child node.
        /// </value>
        /// Creator:LiLei
        /// CreationDate:12/18/2012 2:57 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string ErrorsConfigFileChildNodeName
        {
            get { return GetAppSetting("ErrorsConfigChildNodeName"); }
        }

        /// <summary>
        /// Gets the sys configuration file.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/14/2012 5:21 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string SysConfigurationFile
        {
            get
            {
                return GetAppSetting("SysConfigurationFile");
            }
        }

        /// <summary>
        /// Gets the XML configuration file.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/14/2012 5:22 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string XmlConfigurationFile
        {
            get { return GetAppSetting("XmlConfigurationFile"); }
        }

        /// <summary>
        /// Gets the name of the XML configuration root node.
        /// </summary>
        /// <value>
        /// The name of the XML configuration root node.
        /// </value>
        /// Creator:LiLei
        /// CreationDate:7/14/2012 5:22 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string XmlConfigurationRootNodeName
        {
            get
            {
                return GetAppSetting("XmlConfigurationRootNodeName");
            }
        }

        /// <summary>
        /// Gets the N hibernate CFG path.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/22/2012 3:21 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string NHibernateCfgPath
        {
            get
            {
                return GetAppSetting("NHibernateConfigPath");
            }
        }

        /// <summary>
        /// Gets the log4 net CFG path.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:7/22/2012 3:21 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string Log4NetCfgPath
        {
            get
            {
                return GetAppSetting("Log4netConfigPath");
            }
        }

        /// <summary>
        /// Gets the IOC component CFG path.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:11/2/2012 3:30 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string IOCComponentCfgPath
        {
            get
            {
                return GetAppSetting("IOCComponentConfigPath");
            }
        }

        /// <summary>
        /// Gets the fluent N hibernate assembly.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:11/2/2012 3:30 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string FluentNHibernateAssembly
        {
            get
            {
                return GetAppSetting("FluentNHibernateAssembly");
            }
        }


        /// <summary>
        /// Gets the application logger.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:12/3/2012 7:40 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string ApplicationLogger
        {
            get
            {
                return GetAppSetting("ApplicationLogger");
            }
        }

        /// <summary>
        /// Gets the operations config path.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:12/3/2012 7:41 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string OperationsConfigPath
        {
            get
            {
                return GetAppSetting("OperationsConfigPath");
            }
        }

        /// <summary>
        /// Gets the name of the operations config root.
        /// </summary>
        /// <value>
        /// The name of the operations config root.
        /// </value>
        /// Creator:LiLei
        /// CreationDate:12/3/2012 7:44 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string OperationsConfigRootName
        {
            get
            {
                return GetAppSetting("OperationsConfigRootName");
            }
        }

        /// <summary>
        /// Gets the db connection.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:12/12/2012 6:00 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string DbConnection
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString;
            }
        }

        /// <summary>
        /// Gets the test user account.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:12/18/2012 2:19 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string TestUserAccount
        {
            get
            {
                return GetAppSetting("TestUserAccount");
            }
        }

        /// <summary>
        /// Gets the test user password.
        /// </summary>
        /// Creator:LiLei
        /// CreationDate:12/18/2012 2:19 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        public static string TestUserPassword
        {
            get
            {
                return GetAppSetting("TestUserPassword");
            }
        }


        /// <summary>
        /// Gets the app setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// Creator:LiLei
        /// CreationDate:12/12/2012 6:01 PM
        /// Modify:
        /// ModifyDate:
        /// ----------------------------------------------------------------------------------------
        private static string GetAppSetting(string key)
        {
            //return ConfigurationManager.AppSettings[key];
            var configPath = string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory, "Config/SysConfig.cfg.xml");
            var xmlDocument = XDocument.Load(configPath);

            if (xmlDocument.Root == null)
            {
                return string.Empty;
            }
            var xElement = xmlDocument.Descendants("add").FirstOrDefault(p => p.FirstAttribute.Value==key);

            if (xElement == null)
                return string.Empty;

            return xElement.LastAttribute.Value;
        }
    }
}
